package com.spsy.api;

import com.spsy.common.core.domain.R;
import com.spsy.domain.Orders;
import com.spsy.order.api.OrderApi;
import com.spsy.service.IOrderService;
import com.spsy.vo.NotifyPay2OrderVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Slf4j
public class OrderApiController implements OrderApi {

    @Autowired
    private IOrderService iOrderService;


    @Override
    public R weixin(NotifyPay2OrderVo notifyPay2OrderVo) throws Exception {

        List<Orders> orders = iOrderService.selectOrderList(Orders.builder().orderNo(notifyPay2OrderVo.getOrderId()).build());
        if (orders == null && orders.size() <= 1) {
            return R.fail("订单不存在");
        }

        for (Orders order : orders) {
            if(order.getOrderStatus().equals(2)){
                return R.fail("该订单已支付成功");
            }
        }

        int i = iOrderService.updateOrderByOrderId(Orders.builder().orderNo(notifyPay2OrderVo.getOrderId()).orderStatus(notifyPay2OrderVo.getStatus()).build());

        log.info("count:{}",i);

        return R.ok();
    }
}
